package com.winit.openapi.sms;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.winit.common.spi.context.CommandContext;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.sms.common.SmsUtils;
import com.winit.sms.spi.base.bankaccount.BankAccountService;
import com.winit.sms.spi.base.bankaccount.command.QueryBankAccountByOrgIdCommand;
import com.winit.sms.spi.base.bankaccount.command.QueryBankAccountByPayChannelCommand;
import com.winit.sms.spi.base.bankaccount.vo.BankAccountVo;
import com.winit.sms.spi.base.payaccount.PayAccountService;
import com.winit.sms.spi.base.payaccount.vo.PayAccountVo;
import com.winit.sms.spi.common.command.IdCommand;

/**
 * 银行账号
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * keke.xu 	1.0  		2016年1月12日 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/sms/bankAccount")
public class BankAccountController extends BaseController {

    private static Logger      logger = LoggerFactory.getLogger(BankAccountController.class);
    @Resource
    private BankAccountService bankAcctService;

    @Resource
    private PayAccountService  payAccountService;

    /**
     * 根据支付渠道、币种、组织id查询银帐号
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/queryBankAccountByPayChannel")
    public String queryBankAccountByPayChannel(@ModelAttribute RequestMsg requestMsg,
                                               @ModelAttribute ResponseMsg responseMsg) {
        QueryBankAccountByPayChannelCommand command = SmsUtils.toJavaObject(requestMsg.getData(),
            QueryBankAccountByPayChannelCommand.class);
        logger.info(">>>>>>>>>>>>>>>>>>>>>>>>queryBankAccountByPayChannel:" + command.toString());
        CommandContext ctx = CommandContext.getContext();
        command.setCtx(ctx);
        BankAccountVo bankAccountVo = bankAcctService.queryBankAccountByPayChannel(command);
        if (bankAccountVo != null) {
            Long payAccountId = bankAccountVo.getPayAccountId();
            if (payAccountId != null) {
                IdCommand id = new IdCommand();
                id.setId(bankAccountVo.getPayAccountId());
                PayAccountVo payAccountVo = payAccountService.queryPayAccountById(id);
                Map<String, Object> paramsMap = new HashMap<String, Object>();
                paramsMap.put("payAccount", payAccountVo);
                paramsMap.put("accountNo", bankAccountVo.getAccountNo());
                paramsMap.put("accountId", bankAccountVo.getId());
                responseMsg.setData(paramsMap);
            }
        }
        return SUCCESS;
    }

    /**
     * 根据组织id查询线下银行账户列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/queryBankAccountByOrgId")
    public String queryBankAccountByOrgId(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        try {
            QueryBankAccountByOrgIdCommand command = SmsUtils.toJavaObject(requestMsg.getData(),
                QueryBankAccountByOrgIdCommand.class);
            CommandContext ctx = CommandContext.getContext();
            command.setCtx(ctx);
            List<BankAccountVo> list = bankAcctService.queryBankAccountByOrgId(command);
            responseMsg.setData(list);
        } catch (Exception e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return SUCCESS;
    }
}
